home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / ucbmail / quit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-03  |  8.0 KB  |  352 lines

  1. /*
  2.  *  Q U I T . C 
  3.  *
  4.  *  EE/CIS Computer Lab
  5.  *  Department of Computer and Information Sciences
  6.  *  Department of Electrical Engineering
  7.  *  University of Delaware
  8.  *
  9.  *  REVISION HISTORY:
  10.  *
  11.  *  $Revision: 1.3 $
  12.  *
  13.  *  $Log:    quit.c,v $
  14.  * Revision 1.3  85/12/18  13:25:49  galvin
  15.  * Add another argument to send to indicate whether or not this
  16.  * message should be delimited by MMDF message delimiters.
  17.  * 
  18.  * Change all but "temp" file opens/closes to use MMDF locking routines.
  19.  * 
  20.  * Revision 1.2  85/12/18  03:18:48  galvin
  21.  * Added comment header for revision history.
  22.  * 
  23.  *
  24.  */
  25.  
  26. /*
  27.  * Copyright (c) 1980 Regents of the University of California.
  28.  * All rights reserved.  The Berkeley software License Agreement
  29.  * specifies the terms and conditions for redistribution.
  30.  */
  31.  
  32. #ifndef lint
  33. static char *sccsid = "@(#)quit.c    5.2 (Berkeley) 6/21/85";
  34. #endif not lint
  35.  
  36. #include "./rcv.h"
  37. #include <sys/stat.h>
  38. #include <sys/file.h>
  39. #include "./mmdf.h"
  40.  
  41. /*
  42.  * Rcv -- receive mail rationally.
  43.  *
  44.  * Termination processing.
  45.  */
  46.  
  47. /*
  48.  * Save all of the undetermined messages at the top of "mbox"
  49.  * Save all untouched messages back in the system mailbox.
  50.  * Remove the system mailbox, if none saved there.
  51.  */
  52.  
  53. quit()
  54. {
  55.     int mcount, p, modify, autohold, anystat, holdbit, nohold;
  56.     FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat, *abuf;
  57.     register struct message *mp;
  58.     register int c;
  59.     extern char tempQuit[], tempResid[];
  60.     struct stat minfo;
  61.     char *id;
  62.  
  63.     /*
  64.      * If we are read only, we can't do anything,
  65.      * so just return quickly.
  66.      */
  67.  
  68.     if (readonly)
  69.         return;
  70.     /*
  71.      * See if there any messages to save in mbox.  If no, we
  72.      * can save copying mbox to /tmp and back.
  73.      *
  74.      * Check also to see if any files need to be preserved.
  75.      * Delete all untouched messages to keep them out of mbox.
  76.      * If all the messages are to be preserved, just exit with
  77.      * a message.
  78.      *
  79.      * If the luser has sent mail to himself, refuse to do
  80.      * anything with the mailbox, unless mail locking works.
  81.      */
  82.  
  83.     if ((fbuf = lk_fopen(mailname, "r", (char *) 0, (char *) 0, 5)) == NULL)
  84.         goto newmail;
  85.     rbuf = NULL;
  86.     if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
  87.         printf("New mail has arrived.\n");
  88.         rbuf = fopen(tempResid, "w");
  89.         if (rbuf == NULL || fbuf == NULL)
  90.             goto newmail;
  91. #ifdef APPEND
  92.         fseek(fbuf, mailsize, 0);
  93.         while ((c = getc(fbuf)) != EOF)
  94.             putc(c, rbuf);
  95. #else
  96.         p = minfo.st_size - mailsize;
  97.         while (p-- > 0) {
  98.             c = getc(fbuf);
  99.             if (c == EOF)
  100.                 goto newmail;
  101.             putc(c, rbuf);
  102.         }
  103. #endif
  104.         fclose(rbuf);
  105.         if ((rbuf = fopen(tempResid, "r")) == NULL)
  106.             goto newmail;
  107.         remove(tempResid);
  108.     }
  109.  
  110.     /*
  111.      * Adjust the message flags in each message.
  112.      */
  113.  
  114.     anystat = 0;
  115.     autohold = value("hold") != NOSTR;
  116.     holdbit = autohold ? MPRESERVE : MBOX;
  117.     nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
  118.     if (value("keepsave") != NOSTR)
  119.         nohold &= ~MSAVED;
  120.     for (mp = &message[0]; mp < &message[msgCount]; mp++) {
  121.         if (mp->m_flag & MNEW) {
  122.             mp->m_flag &= ~MNEW;
  123.             mp->m_flag |= MSTATUS;
  124.         }
  125.         if (mp->m_flag & MSTATUS)
  126.             anystat++;
  127.         if ((mp->m_flag & MTOUCH) == 0)
  128.             mp->m_flag |= MPRESERVE;
  129.         if ((mp->m_flag & nohold) == 0)
  130.             mp->m_flag |= holdbit;
  131.     }
  132.     modify = 0;
  133.     if (Tflag != NOSTR) {
  134.         if ((readstat = fopen(Tflag, "w")) == NULL)
  135.             Tflag = NOSTR;
  136.     }
  137.     for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
  138.         if (mp->m_flag & MBOX)
  139.             c++;
  140.         if (mp->m_flag & MPRESERVE)
  141.             p++;
  142.         if (mp->m_flag & MODIFY)
  143.             modify++;
  144.         if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
  145.             id = hfield("article-id", mp);
  146.             if (id != NOSTR)
  147.                 fprintf(readstat, "%s\n", id);
  148.         }
  149.     }
  150.     if (Tflag != NOSTR)
  151.         fclose(readstat);
  152.     if (p == msgCount && !modify && !anystat) {
  153.         if (p == 1)
  154.             printf("Held 1 message in %s\n", mailname);
  155.         else
  156.             printf("Held %2d messages in %s\n", p, mailname);
  157.         lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  158.         if (rbuf != NULL)
  159.             fclose(rbuf);
  160.         return;
  161.     }
  162.     if (c == 0) {
  163.         if (p != 0) {
  164.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  165.             writeback(rbuf);
  166.             return;
  167.         }
  168.         goto cream;
  169.     }
  170.  
  171.     /*
  172.      * Create another temporary file and copy user's mbox file
  173.      * darin.  If there is no mbox, copy nothing.
  174.      * If he has specified "append" don't copy his mailbox,
  175.      * just copy saveable entries at the end.
  176.      */
  177.  
  178.     mcount = c;
  179.     if (value("append") == NOSTR) {
  180.         if ((obuf = fopen(tempQuit, "w")) == NULL) {
  181.             perror(tempQuit);
  182.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  183.             return;
  184.         }
  185.         if ((ibuf = fopen(tempQuit, "r")) == NULL) {
  186.             perror(tempQuit);
  187.             remove(tempQuit);
  188.             fclose(obuf);
  189.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  190.             return;
  191.         }
  192.         remove(tempQuit);
  193.         if ((abuf = fopen(mbox, "r")) != NULL) {
  194.             while ((c = getc(abuf)) != EOF)
  195.                 putc(c, obuf);
  196.             fclose(abuf);
  197.         }
  198.         if (ferror(obuf)) {
  199.             perror(tempQuit);
  200.             fclose(ibuf);
  201.             fclose(obuf);
  202.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  203.             return;
  204.         }
  205.         fclose(obuf);
  206.         close(creat(mbox, sentprotect));
  207.         if ((obuf = fopen(mbox, "r+")) == NULL) {
  208.             perror(mbox);
  209.             fclose(ibuf);
  210.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  211.             return;
  212.         }
  213.     }
  214.     if (value("append") != NOSTR)
  215.         if ((obuf = fopen(mbox, "a")) == NULL) {
  216.             perror(mbox);
  217.             lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  218.             return;
  219.         }
  220.     for (mp = &message[0]; mp < &message[msgCount]; mp++)
  221.         if (mp->m_flag & MBOX)
  222.             if (send(mp, obuf, 0, 1) < 0) {
  223.                 perror(mbox);
  224.                 if (ibuf != NULL)
  225.                 fclose(ibuf);
  226.                 fclose(obuf);
  227.                 lk_fclose(fbuf, mailname, (char *)0, (char *)0);
  228.                 return;
  229.             }
  230.  
  231.     /*
  232.      * Copy the user's old mbox contents back
  233.      * to the end of the stuff we just saved.
  234.      * If we are appending, this is unnecessary.
  235.      */
  236.  
  237.     if (value("append") == NOSTR) {
  238.         rewind(ibuf);
  239.         c = getc(ibuf);
  240.         while (c != EOF) {
  241.             putc(c, obuf);
  242.             if (ferror(obuf))
  243.                 break;
  244.             c = getc(ibuf);
  245.         }
  246.         fclose(ibuf);
  247.         fflush(obuf);
  248.     }
  249.     if (ferror(obuf)) {
  250.         perror(mbox);
  251.         fclose(obuf);
  252.         lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  253.         return;
  254.     }
  255.     fclose(obuf);
  256.     if (mcount == 1)
  257.         printf("Saved 1 message in mbox\n");
  258.     else
  259.         printf("Saved %d messages in mbox\n", mcount);
  260.  
  261.     /*
  262.      * Now we are ready to copy back preserved files to
  263.      * the system mailbox, if any were requested.
  264.      */
  265.  
  266.     if (p != 0) {
  267.         lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  268.         writeback(rbuf);
  269.         return;
  270.     }
  271.  
  272.     /*
  273.      * Finally, remove his /usr/mail file.
  274.      * If new mail has arrived, copy it back.
  275.      */
  276.  
  277. cream:
  278.     if (rbuf != NULL) {
  279.         lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  280.         if ((abuf = lk_fopen(mailname, "w+", (char *) 0, (char *) 0, 5)) == NULL)
  281.             goto newmail;
  282.         while ((c = getc(rbuf)) != EOF)
  283.             putc(c, abuf);
  284.         fclose(rbuf);
  285.         lk_fclose(abuf, mailname, (char *) 0, (char *) 0);
  286.         alter(mailname);
  287.         return;
  288.     }
  289.     demail();
  290.     lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  291.     return;
  292.  
  293. newmail:
  294.     printf("Thou hast new mail.\n");
  295.     if (fbuf != NULL)
  296.         lk_fclose(fbuf, mailname, (char *) 0, (char *) 0);
  297. }
  298.  
  299. /*
  300.  * Preserve all the appropriate messages back in the system
  301.  * mailbox, and print a nice message indicated how many were
  302.  * saved.  On any error, just return -1.  Else return 0.
  303.  * Incorporate the any new mail that we found.
  304.  */
  305. writeback(res)
  306.     register FILE *res;
  307. {
  308.     register struct message *mp;
  309.     register int p, c;
  310.     FILE *obuf;
  311.  
  312.     p = 0;
  313.     if ((obuf = lk_fopen(mailname, "w+", (char *) 0, (char *) 0, 5)) == NULL) {
  314.         perror(mailname);
  315.         return;  /* error */
  316.     }
  317. #ifndef APPEND
  318.     if (res != NULL)
  319.         while ((c = getc(res)) != EOF)
  320.             putc(c, obuf);
  321. #endif
  322.     for (mp = &message[0]; mp < &message[msgCount]; mp++)
  323.         if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
  324.             p++;
  325.             if (send(mp, obuf, 0, 1) < 0) {
  326.                 perror(mailname);
  327.                 lk_fclose(obuf, mailname, (char *) 0, (char *) 0);
  328.                 return;   /* error */
  329.             }
  330.         }
  331. #ifdef APPEND
  332.     if (res != NULL)
  333.         while ((c = getc(res)) != EOF)
  334.             putc(c, obuf);
  335. #endif
  336.     fflush(obuf);
  337.     if (ferror(obuf)) {
  338.         perror(mailname);
  339.         lk_fclose(obuf, mailname, (char *) 0, (char *) 0);
  340.         return;   /* error */
  341.     }
  342.     if (res != NULL)
  343.         fclose(res);
  344.     lk_fclose(obuf, mailname, (char *) 0, (char *) 0);
  345.     alter(mailname);
  346.     if (p == 1)
  347.         printf("Held 1 message in %s\n", mailname);
  348.     else
  349.         printf("Held %d messages in %s\n", p, mailname);
  350.     return;
  351. }
  352.